package com.apache.common.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.ius.plugin.UtilPlugin;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台部分数据统计
 * 服务bean、功能、数据表、接口、插件、数据字典、管理员
 */
public class PlateformDataStatisticsPluginImpl implements CustomMethodPlugin {
    protected Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        String sysEname = String.valueOf(param.get("searchSysEname"));
        log.info("平台系统["+sysEname+"]功能数据统计....");

        String beanServerId = "";
        String whereSysNameStr = "";
        String whereSysEnameStr = "";
        String whereSysNoStr = "";
        if (StrUtil.isNotNull(sysEname)){
            whereSysNameStr = " and sys_name='"+sysEname+"'";
            whereSysEnameStr = " and sys_ename='"+sysEname+"'";
            whereSysNoStr = " and sys_no='"+sysEname+"'";
            beanServerId = sysEname+"Service";
        }

        StringBuffer sql = new StringBuffer("");
        sql.append("select ");
        sql.append("count(*) as rpc_server_count,");
        sql.append("(select count(*) from sys_module where 1=1 "+whereSysNameStr+") as sys_module_count,");
        sql.append("(select count(*) from pub_sort_info where 1=1 "+whereSysNameStr+") as sort_info_count,");
        sql.append("(select count(*) from sys_param_manager where 1=1 "+whereSysNameStr+") as param_manager_count,");
        sql.append("(select count(*) from sys_plugin_manager where 1=1 "+whereSysNameStr+") as plugin_manager_count,");
        sql.append("(select count(*) from dict_data_cate where 1=1 "+whereSysEnameStr+") as data_cate_count,");
//        sql.append("(select count(*) from sys_rules_prod where 1=1 "+whereSysNameStr+") as sys_rules_prod_count,");
        sql.append("(select count(*) from uct_user_sys where 1=1 "+whereSysNoStr+") as uct_user_sys_count");
        sql.append(" from t_interface_register t where t.bean_id='"+beanServerId+"'");

        Object resultEntity = IusPluginUtil.getInstance().select("plateform",sql.toString(),0,0);
        List<DataMap> resultEntityList = (List<DataMap>)resultEntity;

        Map<String, Object> result = new HashMap<>(2);
        result.put(CustomMethodPlugin.resultMark, "查询成功");
        result.put(CustomMethodPlugin.pluginInnerReturn, resultEntityList);

        return result;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        String orgId = String.valueOf(param.get("ids"));
        if ( Validator.isNotNull(orgId)) {
            Map<String,Object> deptMap = new HashMap<String,Object>();
            deptMap.put("uctDept.deptId",orgId);
            deptMap.put("uctDept.deptEname",orgId);
            deptMap.put("uctDept.deptCname","会员部");
            deptMap.put("uctDept.deptStatus",1);
            deptMap.put("uctDept.orgId",orgId);
            deptMap.put("uctDept.fatherId","0");
            deptMap.put("uctDept.orgEname",orgId);
            ResultEntity resultEntity = UtilPlugin.getInstance().saveByParams("platform","uctDept",deptMap);
        }
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

    public static void main(String[] args) {
        Date date = new Date();
        String createTime = DateUtils.getTime(date.toString());
        System.out.println(createTime);
    }
}
